/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.projetjava.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import fr.unice.miage.projetjava.entities.Produit;
import java.util.Collection;

/**
 *
 * @author Olivier
 */
public class AffichageCentral extends Composite {

    private ScrollPanel panel = new ScrollPanel();
    private HTML page = new HTML();
    private String temp = new String();
    private FlexTable table = new FlexTable();
    private Collection<Produit> listProd = null;

    public AffichageCentral()
    {
        final AsyncCallback callbackProduit = new AsyncCallback() {

            public void onSuccess(Object result) {
                listProd = (Collection<Produit>)result;
                if(listProd.size()==0)
                {
                    page.setText("Catalogue vide...");
                }
                else
                {
                    final AsyncCallback callbackAdd = new AsyncCallback() {

                        public void onSuccess(Object result) {
                            Popup p = new Popup("Produit ajouté au panier", "auto");
                            int left = table.getAbsoluteLeft() + 10;
                            int top = table.getAbsoluteTop() + 10;
                            p.setPopupPosition(left, top);
                            p.show();
                        }

                        public void onFailure(Throwable caught) {
                            Popup p = new Popup("Suite à un problème, le produit n'a pu être ajouté à votre panier", "auto");
                            int left = table.getAbsoluteLeft() + 10;
                            int top = table.getAbsoluteTop() + 10;
                            p.setPopupPosition(left, top);
                            p.show();
                        }
                    };

                    int i = 0;
                    for(final Produit p : listProd)
                    {
                        //table.setHTML(i, 0, "<img src='"+p.getPicture()+".jpg'>");
                        Image image = new Image(p.getPicture()+".jpg");
                        table.setWidget(i, 0, image);
                        table.setText(i, 1, p.getProduct_name());
                        table.setText(i, 3, String.valueOf(p.getProduct_price()));
                        Button button = new Button("Commander", new ClickListener(){
                            public void onClick(Widget w) {
                            // Make remote call. Control flow will continue immediately and later
                            // 'callback' will be invoked when the RPC completes.
                            getService().ajouterProduit(p, callbackAdd);
                        }});
                        table.setWidget(i, 4, button);
                        i++;
                    }
                    temp += "</table>";
                }
                page.setHTML(temp);
            }

            public void onFailure(Throwable caught) {
                page.setText("Erreur lors du chargement des produits");
            }
        };

        getService().affichageProduit(callbackProduit);

        panel.add(table);
        panel.setHeight("80%");
        panel.setWidth("80%");
        initWidget(panel);
    }

    public HTML getPage() {
        return page;
    }

    public void setPage(HTML page) {
        this.page = page;
    }

    public static MenuClientTestAsync getService(){
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        MenuClientTestAsync service = (MenuClientTestAsync) GWT.create(MenuClientTest.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "menuclienttest";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}
